package website.seamonster.ethegg.service;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import website.seamonster.ethegg.dataobject.Msg;
import website.seamonster.ethegg.dto.BagDTO;
import website.seamonster.ethegg.dto.MsgDTO;
import website.seamonster.ethegg.enums.MsgTypeEnum;
import website.seamonster.ethegg.repository.MsgRepository;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 罗海量
 * 2018-07-29 17:01
 */
@lombok.extern.slf4j.Slf4j
@Service
public class MsgService {
    @Autowired
    private MsgRepository msgRepository;

    public List<MsgDTO> getMsgList(String userId, Integer pageCount){
        List<Msg> msgList = msgRepository.findAllByUserIdOrderByCreateTimeDesc(userId, new PageRequest(pageCount, 30)).getContent();
        List<MsgDTO> msgDTOList = msgList.stream().map(msg -> {
            MsgDTO dto = new MsgDTO();
            BeanUtils.copyProperties(msg, dto);
            return dto;
        }).collect(Collectors.toList());
        return msgDTOList;
    }

    public void createMsg(String userId, BigDecimal balance,
                          MsgTypeEnum msgType, Timestamp createTime,
                          Long blockNum){
        List<Msg> list = msgRepository.findAllByUserIdAndMsgTypeAndCreateTime(userId, msgType.getCode(), createTime);
        if (list!=null && list.size()>0){
            //duplicate
            return;
        } else{
            // create message
            Msg msg = new Msg();
            msg.setUserId(userId);
            msg.setIncome(balance);
            msg.setMsgType(msgType.getCode());
            msg.setCreateTime(createTime);
            msg.setBlockNum(blockNum);
            msgRepository.save(msg);
        }
    }

    @Transactional
    public void createMsgs(List<Msg> msgList){
        msgRepository.save(msgList);
//        if (msgList == null){
//            return;
//        }
//        msgList.forEach(msg -> {
//            List<Msg> list = msgRepository.findAllByUserIdAndMsgTypeAndCreateTime(msg.getUserId(), msg.getMsgType(), msg.getCreateTime());
//            if (list!=null && list.size()>0){
//                //duplicate
//            } else {
//                msgRepository.save(msg);
//            }
//        });
    }

    public Long getNextBlockNum(){
        Long blockNum = msgRepository.getMaxBlockNum();
        if (blockNum==null || blockNum == 0){
            return 0L;
        }else {
            return blockNum + 1L;
        }
    }

    public boolean isUserHasBuyEgg(String accountAddress){
        Msg m =msgRepository.getFirstByUserIdAndMsgTypeOrderByBlockNumAsc(accountAddress, MsgTypeEnum.BUYSUCCESS.getCode());
        return m!=null;
    }

    public BagDTO getIncomeInfo(String accountAddress){
        BagDTO bagDTO = new BagDTO();

        //销售收益
        bagDTO.setSellIncome(msgRepository.sumIncome(accountAddress, MsgTypeEnum.SELLSUCCESS.getCode()));

        //推广码收益
        bagDTO.setPrIncome(msgRepository.sumIncome(accountAddress, MsgTypeEnum.PRINCOME.getCode()));

        //购买支出
        bagDTO.setBuyExpense(msgRepository.sumIncome(accountAddress, MsgTypeEnum.BUYSUCCESS.getCode()));

        if (bagDTO.getSellIncome() == null) {
            bagDTO.setSellIncome(BigDecimal.ZERO);
        }

        if (bagDTO.getPrIncome() == null) {
            bagDTO.setPrIncome(BigDecimal.ZERO);
        }

        if (bagDTO.getBuyExpense() == null){
            bagDTO.setBuyExpense(BigDecimal.ZERO);
        }

        return bagDTO;
    }
}
